package com.cqq.homepage.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.cqq.auth.service.PermResourceService;
import com.cqq.auth.service.bean.MenuBean;
import com.cqq.auth.service.exception.PermResourceServiceException;
import com.cqq.base.filter.FMContext;
import com.cqq.common.core.base.BaseController;
import com.cqq.common.core.constant.SessionConstant;


@Controller(value = "HeadMenuController")
@RequestMapping(value = { "/headMenu" })
public class HeadMenuController  extends BaseController{

	@Autowired
	private PermResourceService permResourceService;

	@Value("${system.code}")
	private String systemCode;
	
	@RequestMapping(value = { "queryTopMenuList" })
	public String queryAllTopMenusByUserId(HttpServletRequest request, HttpServletResponse response,  ModelMap out) {

		List<MenuBean> listMenuSys = this.permResourceService.listMenu(FMContext.getCurrent().getUserId(), systemCode, true);
		out.put("topmenus", listMenuSys);
		
		return "/common/moreMenu";
	}
	
    @RequestMapping(value ={"doStatusFlag"})
    public void doStatusFlag(HttpServletRequest request, HttpServletResponse response, ModelMap out)
    {
        JSONObject json = new JSONObject();
		int id = Integer.valueOf(request.getParameter("id"));
		boolean needShow = Boolean.valueOf(request.getParameter("needShow"));
		int index = Integer.valueOf(request.getParameter("index"));
		//接收和格式化页面传递的参数 结束

		MenuBean mb = new MenuBean();
		mb.setId(id);
		mb.setIndex(index);
		mb.setSystemCode(systemCode);
		mb.setNeedShow(!needShow);

		List<MenuBean> menus = new ArrayList<MenuBean>();
		menus.add(mb);
		try {
			permResourceService.saveUserMenuConfig(FMContext.getCurrent().getUserId(), menus,FMContext.getCurrent().getSystemCode());           
			json.put("result", "success");
		} catch (PermResourceServiceException e) {
			e.printStackTrace();
			json.put("result",  e.toString());
		}
		
        outJsonString(response, json);

    }	
    
    @RequestMapping(value ={"updateTopMenus"})
    public void updateTopMenus(HttpServletRequest request, HttpServletResponse response, ModelMap out)
    {
        JSONObject json = new JSONObject();
        List<MenuBean> menus=permResourceService.listMenu(FMContext.getCurrent().getUserId(), systemCode);
        request.getSession().setAttribute(SessionConstant.MENUS, menus);
        json.put("result", "success");
        outJsonString(response, json);
    }	   
    public void outJsonString(HttpServletResponse response, JSONObject json)
    {
        response.setContentType("text/javascript;charset=UTF-8");
        response.setHeader("Cache-Control", "no-store, max-age=0, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        try
        {
            PrintWriter out = response.getWriter();
            out.write(json.toString());
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
  
    
    
}